package cn.rdq.iknessplatform.controller;

import cn.rdq.api.dto.BlogCommentInfoDto;
import cn.rdq.api.dto.BlogDetailDto;
import cn.rdq.api.po.Blog;
import cn.rdq.api.vo.BlogVo;
import cn.rdq.common.CommonResult;
import cn.rdq.iknessplatform.service.BlogCommentService;
import cn.rdq.iknessplatform.service.BlogService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog")
@Api
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogCommentService blogCommentService;

    @GetMapping("/{id}")
    public CommonResult getById(@PathVariable Long id) {
        blogService.viewsIncrease(id);
        BlogDetailDto blog = blogService.getDtoById(id);
        List<BlogCommentInfoDto> comments = blogCommentService.rootComment(id);
        blog.setComments(comments);
        return CommonResult.successObj(blog);
    }

    @PostMapping("/publish")
    public CommonResult publishBlog(@RequestBody Blog blog) {
        Blog publishBlog = blogService.publishBlog(blog);
        return CommonResult.successObj(publishBlog);
    }

    @PostMapping("/list/brief")
    public CommonResult listBrief(@RequestBody @Validated BlogVo blogVo) {
        return CommonResult.success().pageSuccess(blogService.listBlogBrief(blogVo));
    }

    @GetMapping("/popular")
    public CommonResult popular() {
        return CommonResult.successObj(blogService.hotBlog());
    }

    @GetMapping("/del/{id}")
    public CommonResult delete(@PathVariable Long id) {
        blogService.removeById(id);
        return CommonResult.success();
    }
}
